﻿using System.Collections.Generic;
using System.Linq;
using Abp.Configuration;
using Microsoft.Extensions.Configuration;

namespace Partner.Surround.Configuration
{
    public class AppSettingProvider : SettingProvider
    {
        #region 初始化
        private readonly IConfigurationRoot _appConfiguration;

        public AppSettingProvider(IAppConfigurationAccessor configurationAccessor)
        {
            _appConfiguration = configurationAccessor.Configuration;
        }
        #endregion

        public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context)
        {
            var settings = GetUISettings()
                .Union(GetHostSettings());

            return settings;
        }

        /// <summary>
        /// 获取UI设置
        /// </summary>
        /// <returns></returns>
        private IEnumerable<SettingDefinition> GetUISettings()
        {
            return new[] {
                new SettingDefinition(
                    AppSettingNames.UiTheme,
                    "red",
                    scopes: SettingScopes.Application | SettingScopes.Tenant | SettingScopes.User,
                    isVisibleToClients: true
                 )
            };
        }

        /// <summary>
        /// 获取系统管理设置
        /// </summary>
        /// <returns></returns>
        private IEnumerable<SettingDefinition> GetHostSettings()
        {
            return new[] {
                new SettingDefinition(
                        AppSettingNames.HostManagement.CompanyName,
                        GetFromAppSettings(AppSettingNames.HostManagement.CompanyName)
                    ),
                new SettingDefinition(
                        AppSettingNames.HostManagement.CompanyAddress,
                        GetFromAppSettings(AppSettingNames.HostManagement.CompanyAddress)
                    ),
            };
        }

        #region 辅助方法
        private string GetFromAppSettings(string name, string defaultValue = null)
        {
            return GetFromSettings("App:" + name, defaultValue);
        }

        private string GetFromSettings(string name, string defaultValue = null)
        {
            return _appConfiguration[name] ?? defaultValue;
        }
        #endregion
    }
}
